<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>内部订单添加</title>
	<meta name="decorator" content="default"/>
</head>
<body>
	 <ul class="nav nav-tabs">
		<li><a href="${ctx}/bu/buInternalOrder/">内部订单列表</a></li>
				<c:choose>
				   <c:when test="${flag eq 1 and write eq 0}">
				   		<shiro:hasPermission name="bu:buInternalOrder:operate:edit"><li class="active"><a href="${ctx}/bu/buInternalOrder/form?id=${buOrder.id}&flag=1&write=0">内部订单${not empty buOrder.id?'修改':'添加'}</a></li></shiro:hasPermission>
				   </c:when>
				   <c:when test="${flag eq 2 and write eq 0}">
				   		<shiro:hasPermission name="bu:buInternalOrder:view"><li class="active"><a href="${ctx}/bu/buInternalOrder/view?id=${buOrder.id}&flag=2&write=0">内部订单查看</a></li></shiro:hasPermission>
				   </c:when>
				   <c:when test="${flag eq 3 and write eq 0}">
				   		<shiro:hasPermission name="bu:buInternalOrder:operate:copy"><li class="active"><a href="${ctx}/bu/buInternalOrder/copy?id=${buOrder.id}&flag=3&write=0">内部订单复制</a></li></shiro:hasPermission>
				   </c:when>
				   <c:when test="${flag eq 6 and write eq 1}">
				   		<shiro:hasPermission name="bu:buInternalOrder:operate:delivery"><li class="active"><a href="${ctx}/bu/buInternalOrder/selectById?id=${buOrder.id}&flag=6&write=1">内部订单填写快递信息</a></li></shiro:hasPermission>
				   </c:when>
				</c:choose>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="buOrder" action="${ctx}/bu/buInternalOrder/copySave?flag=1&write=0" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input type="hidden" name="flag" id="write" value="${flag}" />
		<input type="hidden" name="write" id="write" value="${write}" />
		<sys:message content="${message}"/>
		<div class="control-group">
			<label class="control-label">订单编号：</label>
			<div class="controls">
				<form:input path="orderNo" readonly="true" htmlEscape="false" maxlength="64" class="input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">生产编号：</label>
			<div class="controls">
				<form:input path="productNo" htmlEscape="false" onkeydown="changeFocus()" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">客户订单编号：</label>
			<div class="controls">
				<form:input path="customerOrderNo" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">出货日期：</label>
			<div class="controls">
				<input name="shipDate" id="shipDate" type="text" readonly="true" maxlength="20" class="required input-medium Wdate "
					value="<fmt:formatDate value="${buOrder.shipDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
					<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">下单日期：</label>
			<div class="controls">
				<form:input path="orderDate" readonly="true" htmlEscape="false" maxlength="64" class="input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>		
		<div class="control-group">
			<label class="control-label">产品名称：</label>
			<div class="controls">
				<form:input path="productName" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">工艺：</label>
			<div class="controls">
				<form:input path="crafts" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
				<label class="control-label">是否初订：</label>
				<div class="controls">
					<form:radiobuttons path="status" items="${fns:getDictList('order_status')}" itemLabel="label" itemValue="value" htmlEscape="false" class="required"/>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
		<div class="control-group">
			<label class="control-label">电镀：</label>
			<div class="controls">
				<form:input path="electroplate" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">背面：</label>
			<div class="controls">
				<form:input path="back" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">包装：</label>
			<div class="controls"> 
				<form:input path="packages" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">配件：</label>
			<div class="controls">
				<form:input path="fitting" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">材质：</label>
			<div class="controls">
				<form:input path="material" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">图片：</label>
			<div class="controls">
				    <input type="file" id="file" name="myfile" onchange="aa()"/>
				    <input type="hidden" name="picUrl" id="picUrl" class="required input-xlarge ">
					<img src="" alt="" id="avatarShow" width="500px" height="750px">
					<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">尺寸：</label>
			<div class="controls">
				<form:input path="productSize" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div><div class="control-group">
			<label class="control-label">厚度：</label>
			<div class="controls">
				<form:input path="thinkness" onkeydown="changeFocus()" htmlEscape="false" maxlength="64" class="required input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">数量：</label>
			<div class="controls">
				<form:input path="quantity" onkeydown="changeFocus()" htmlEscape="false" maxlength="11" class="required input-xlarge " id="quantity"
				onkeyup="value=value.replace(/[^\d]/g,'')"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售单价：</label>
			<div class="controls">
				<form:input path="salesPrice" onkeydown="changeFocus()" onkeyup="keepNumOrDecimal(this)" htmlEscape="false" class="required input-xlarge" id="salesPrice"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售模费：</label>
			<div class="controls">
				<form:input path="salesModelFee" onkeydown="changeFocus()" onkeyup="keepNumOrDecimal(this)" htmlEscape="false" class="required input-xlarge " id="salesModelFee"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">生产单价：</label>
			<div class="controls">
				<form:input path="produceFee" onkeydown="changeFocus()" onkeyup="keepNumOrDecimal(this)" htmlEscape="false" class="required input-xlarge " id="produceFee"/>
			    <span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">生产模费：</label>
			<div class="controls">
				<form:input path="produceModelFee" onkeydown="changeFocus()" onkeyup="keepNumOrDecimal(this)" htmlEscape="false" class="required input-xlarge " id="produceModelFee"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售总价：</label>
			<div class="controls">
				<form:input path="salesTotalFee" readonly="true" htmlEscape="false" class="required input-xlarge " id="salesTotalFee"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">生产总价：</label>
			<div class="controls">
				<form:input path="produceTotalFee" readonly="true" htmlEscape="false" class="required input-xlarge " id="produceTotalFee"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">利润：</label>
			<div class="controls">
				<form:input path="profit" readonly="true" htmlEscape="false" class="required input-xlarge " id="profit" name="profit"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">客户名称：</label>
			<div class="controls">
				<form:select id="salesmanId" name="salesmanId" path="salesmanId" class="required input-xlarge ">
					<form:options items="${entityList}" itemLabel="salesmanName" itemValue="id" htmlEscape="false"/>
				</form:select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">创建人：</label>
			<div class="controls">
				<form:input path="createUser" readonly="true" htmlEscape="false" maxlength="64" class="input-xlarge "/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>	
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
			</div>
		</div>
		<div class="form-actions">
		   <c:if test="${flag == 0 || flag == 1 || flag == 4 || flag == 3}">
		   		<shiro:hasPermission name="bu:buInternalOrder:operate:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
		   </c:if>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
	<script type="text/javascript">
		$(document).ready(function(){
				var picUrl = '${buOrder.picUrl}';
				var picPrefix = '${picPrefix}';
				$("#picUrl").attr("value",picUrl);
				$("#avatarShow").attr("src",picPrefix + picUrl);	
		});
	</script>
	<script type="text/javascript">
		function aa(){
			console.log("aaaaaaaaaaa            ",$("#file").val());
			if($("#file").val() != ""){
				var formData = new FormData();
		        formData.append('file',$("#file")[0].files[0]);    //将文件转成二进制形式
				$.ajax({
						 url:"${ctx}/file/uploadFile",
					      data: formData,   //请求参数
						  type: 'POST',  //请求类型
						  dataType:'text',
						  async:false,
						  contentType: false,//没有设置任何内容类型头信息
						  processData: false, //见jQuery_api详解
						  beforeSend: function(){
							 
						 },
					     success: function(data){
						    var json = eval('(' + data + ')'); 
						    console.log("json          ",json);
						    console.log("json          ",json.preffix);
						    //图片显示
							$("#picUrl").attr("value",json.val);
							$("#avatarShow").attr("src",json.preffix+json.val);	
					     },
					     error: function (jqXHR,textStatus,errorThrown) {
					     	alert("上传失败");
					     },
					 });
			}else{
				console.log("222222222222222222");
			}
		}
	</script>
	<script type="text/javascript">
	 function changSalesMan(v){
		   console.log(v);
			    $.ajax({
			        type : "post",
			        async : false,
			        url : "${ctx}/bu/buInternalOrder/selectSalesList",
			        data : {
			            'id' : v
			        },
			        dataType : "json",
			        success : function(msg) {
			            $("#salesmanId").empty();
			            console.log(msg.salesmanId.length);
			            if (msg.salesmanId.length > 0) {
			                for (var i = 0; i < msg.salesmanId.length; i++) {
			                        var partId = msg.salesmanId[i].id;
			                        var partName = msg.salesmanId[i].salesmanName;
			                        var $option = $("<option>").attr({
			                            "value" : partId
			                        }).text(partName);
			                        $("#salesmanId").append($option);
			                }
			        }else{
			        	$("#salesmanId").empty();
			        }
			    }, error : function(json) {
		            $.jBox.alert("网络异常！");
		        }
			    })
	   }
	
	  function changeFocus(){
	       if(event.keyCode==13)
	          event.keyCode=9;
	    }
	 
		$(document).ready(function() {
			
			console.log("123");
			var customerId = $("#customerId").find("option:selected").val();
			changSalesMan(customerId);
			$("#salesmanId").get(0).selectedIndex;
			$("#salesmanId").prop("disabled", true);
			$("#supplierId").prop("disabled", true);
			$("#customerId").prop("disabled", true);
			
			$("#shipDate").focus();
			$("#productName").focus();
			$("#crafts").focus();
			$("#electroplate").focus();
			$("#back").focus();
			$("#packages").focus();
			$("#fitting").focus();
			$("#picUrl").focus();
			$("#quantity").focus();
			$("#salesPrice").focus();
			
			$("#customerOrderNo").focus();
			
			$("#salesModelFee").focus();
			$("#produceFee").focus();
			$("#produceModelFee").focus();
			$("#salesTotalFee").focus();
			$("#material").focus();
			
			$("#produceTotalFee").focus();
			$("#profit").focus();
			$("#customerId").focus();
			$("#salesmanId").focus();
			$("#supplierId").focus();
			
			$("#productSize").focus();
			$("#thinkness").focus();
			
			$("#productNo").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					$("#salesmanId").prop("disabled", false);
					$("#supplierId").prop("disabled", false);
					$("#customerId").prop("disabled",false);
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
			var sp = 0; 
			var smf = 0;
			$("#quantity").bind("input propertychange",function () {
			  console.log("quantity");
			  var num = $("#quantity").val();
			  var a1 = $("#salesPrice").val();
			  var b1 = $("#salesModelFee").val();
			  var c1 = $("#produceFee").val();
			  var d1 = $("#produceModelFee").val();
			  console.log("num",num);
			  console.log("a1",a1);
			  console.log("b1",b1);
			  console.log("c1",c1);
			  console.log("d1",d1);
			  sp = ((parseFloat(a1) * parseFloat(num)) + parseFloat(b1)).toFixed(2);
			  smf = ((parseFloat(c1) * parseFloat(num)) + parseFloat(d1)).toFixed(2);
			  
			  if(isNaN(smf) && isNaN(smf)){ 
				    smf = 0; 
				 }
			  if(isNaN(sp) && isNaN(sp)){
				  sp = 0;
			  }
			  
			  console.log("sp",sp);
			  console.log("smf",smf);
			  backSp();
			  backSmf();
			});
			
			$("#salesPrice").bind("input propertychange",function(){
				console.log("salesPrice");
				var num = $("#quantity").val();
				var a1 = $("#salesPrice").val();
				var b1 = $("#salesModelFee").val();
				console.log("num",num);
				console.log("a1",a1);
				console.log("b1",b1);
				sp = ((parseFloat(a1) * parseFloat(num)) + parseFloat(b1)).toFixed(2);
				  if(isNaN(sp) && isNaN(sp)){
					  sp = 0;
				  }
				console.log("sp",sp);
				backSp();
			});
			$("#salesModelFee").bind("input propertychange",function(){
				console.log("salesModelFee");
				var num = $("#quantity").val();
				var a1 = $("#salesPrice").val();
				var b1 = $("#salesModelFee").val(); 
				console.log("num",num);
				console.log("a1",a1);
				console.log("b1",b1);
				sp = ((parseFloat(a1) * parseFloat(num)) + parseFloat(b1)).toFixed(2);
			  if(isNaN(sp) && isNaN(sp)){
				  sp = 0;
			  }
				console.log("sp",sp);
				backSp();
			});
			
			$("#produceFee").bind("input propertychange",function () { 
			    console.log("produceFee");
			    var num = $("#quantity").val();
				var a1 = $("#produceFee").val();
				var b1 = $("#produceModelFee").val();  
				smf = ((parseFloat(a1) * parseFloat(num)) + parseFloat(b1)).toFixed(2);
				if(isNaN(smf) && isNaN(smf)){ 
				    smf = 0; 
				 }
				console.log("num",num);
				console.log("a1",a1);
				console.log("b1",b1);
				console.log("smf",smf);
				backSmf();
			});
			
			$("#produceModelFee").bind("input propertychange",function () { 
			    console.log("produceModelFee");
			    var num = $("#quantity").val();
				var a1 = $("#produceFee").val();
				var b1 = $("#produceModelFee").val();  
				console.log("num",num);
				console.log("a1",a1);
				console.log("b1",b1);
				smf = ((parseFloat(a1) * parseFloat(num)) + parseFloat(b1)).toFixed(2);
				if(isNaN(smf) && isNaN(smf)){ 
				    smf = 0; 
				 }
				console.log("smf",smf);
				backSmf();
			});
			
			function backSp(){   
			  $("#salesTotalFee").val(sp);
			  var salesTotalFee = $("#salesTotalFee").val();
			  var produceTotalFee = $("#produceTotalFee").val();
			  var profit = (parseFloat(salesTotalFee) - parseFloat(produceTotalFee)).toFixed(2);
			  console.log("profit",profit);
			  $("#profit").val(profit);
			}
			function backSmf(){
			  $("#produceTotalFee").val(smf);
			  var salesTotalFee = $("#salesTotalFee").val();
			  var produceTotalFee = $("#produceTotalFee").val();
			  var profit = (parseFloat(salesTotalFee) - parseFloat(produceTotalFee)).toFixed(2);
			  console.log("profit",profit);
			  $("#profit").val(profit);
			}
			
		});
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
		// 只能入力数字和小数点  
		function keepNumOrDecimal(obj) {  
		    //先把非数字的都替换掉，除了数字和.  
		    obj.value = obj.value.replace(/[^\d.]/g,"");  
		    //必须保证第一个为数字而不是.  
		    obj.value = obj.value.replace(/^\./g,"");  
		    //保证只有出现一个.而没有多个.  
		    obj.value = obj.value.replace(/\.{2,}/g,".");  
		    //保证.只出现一次，而不能出现两次以上  
		    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");  
		} 
	</script>
</body>
</html>